/*
 * jQuery Notifications plugin 1.1
 *
 * http://programmingmind.com
 *
 * Copyright (c) 2009 David Ang
 *
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *   
 *   Version Changes
 *   
 *   1.1 8/17/2008
 *   - Allow users to choose between slide or fade effect
 *   - Fixed speed option settings   
 *   
 *   1.0 8/9/2008
 *    - Initial release
 */
(function($){
    var template;
    var counter = 0;
	
    $.notifications = function(msg, options) {
        counter++;
        
        var settings = $.extend({}, $.notifications.defaults, options);
        
		if (!template) {
        	template = $('<div id="jquery-notifications"></div>').prependTo(document.body);
		}

		var n = $( '<div class="' + settings.type + '" id="jquery-notifications-' + counter + '">' + msg + '</div>').hide().appendTo("#jquery-notifications");
		
    n.slideDown( settings.speed );

		if (settings.stick) {
			var close = $('<a id="jquery-notifications-close" href="javascript:void(0);">' + settings.close + '</a>').click(function() {
			  $("#jquery-notifications-close").hide();

        $(this.parentNode).slideUp(settings.speed, function ()
			  {
			    $(this).remove();
			  });
			});
			
      close.appendTo(n);
		}		
    	
		if (!settings.stick) {
			var notificationsDelayer = delayTimer(settings.timeout);
			notificationsDelayer(update, { counter: counter, effect: settings.effect, speed : settings.speed } );
		}
	};
	
  $.notifications.notice = function( msg, options ){		
      return $.notifications( msg, $.extend( {}, options, { type : "notice"}) );
  };

	$.notifications.success = function( msg, options ){		
      return $.notifications( msg, $.extend( {}, options, { type : "success"}) );
  };
	
  $.notifications.error = function( msg, options ){
      return $.notifications( msg, $.extend( {}, options, { type : "error" }) );
  };
	
  $.notifications.warning = function( msg, options ){
      return $.notifications( msg, $.extend( {}, options, { type : "warning" }) );
  };

  $.notifications.information = function (msg, options){
      return $.notifications( msg, $.extend( {}, options, { type : "information" }) );
  };

	function update(params) {
	  $("#jquery-notifications-" + params.counter).slideUp(params.speed, function ()
	  {
	    $(this).remove();
	  });	
	}
	
	function delayTimer(delay) {
	    var timer;
	    return function(fn, params) {
	        timer = clearTimeout(timer);
	        if (fn)
	            timer = setTimeout(function() {
	                fn(params);
	            }, delay);
	        return timer;
	    };
	}	

	$.notifications.defaults = {
      type: "information",
			timeout: 5000,
			stick: false,
			speed : "slow",
			close : "x"
  };
    
	$.notify = $.notifications;	 
	
})(jQuery);